import { defineStore } from 'pinia'
import request from '@/utils/request'
import { CategoryItem,ApiRes, TopCategory, SubCategory } from '@/views/types/data'
import { topCategory } from '../constants'
const defaultCategory = topCategory.map(item => ({name:item}))
// interface ApiRes<T>{
//   code:string
//   msg:string
//   result:T[]
// }
// type CategoryItem = {
//   id:number
//   name:string
//   picture:string
// }
export default defineStore('category',{

  state(){
    return{
      list:defaultCategory as CategoryItem[],
      categorylist:{} as TopCategory,
      subCategory :{} as SubCategory
    }
  },
  actions: {
    async getAllCategory(){
     const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
     res.data.result.forEach(item => item.open = false)
     this.list = res.data.result
    //  console.log(res);
    },
  
    show(id: string){
      const chontr = this.list.find(item => item.id === id)
    // if(chontr){
    //      chontr.open = true
    // }
    chontr!.open = true
    },
    hide(id: string){
     const chontr = this.list.find(item => item.id === id)
//      if(chontr){
//       chontr.open = false
//  }
     chontr!.open = false

    },
    async getTopCategory(id:string){
      const res = await request.get<ApiRes<TopCategory>>('/category',{
        params:{
          id
        }
      })
      this.categorylist = res.data.result
    },
    async getfilterlist(id:string){
     const res = await request.get<ApiRes<SubCategory>>('/category/sub/filter',{
      params:{
        id
      }
     })
    //  console.log(res);
       this.subCategory  = res.data.result
    },
    getfiler(){
      this.subCategory = {} as SubCategory
    },
    getTopcat(){
      this.categorylist = {} as TopCategory
    }
  }
})